package com.ruoyi.datascreen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.datascreen.domain.bo.DsLargeScreenBo;
import com.ruoyi.datascreen.domain.vo.DsLargeScreenVo;
import com.ruoyi.datascreen.domain.DsLargeScreen;
import com.ruoyi.datascreen.mapper.DsLargeScreenMapper;
import com.ruoyi.datascreen.service.IDsLargeScreenService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 大屏信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-30
 */
@RequiredArgsConstructor
@Service
public class DsLargeScreenServiceImpl implements IDsLargeScreenService {

    private final DsLargeScreenMapper baseMapper;

    /**
     * 查询大屏信息
     */
    @Override
    public DsLargeScreenVo queryById(Long largeScreenId){
        return baseMapper.selectVoById(largeScreenId);
    }

    /**
     * 查询大屏信息列表
     */
    @Override
    public TableDataInfo<DsLargeScreenVo> queryPageList(DsLargeScreenBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DsLargeScreen> lqw = buildQueryWrapper(bo);
        Page<DsLargeScreenVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询大屏信息列表
     */
    @Override
    public List<DsLargeScreenVo> queryList(DsLargeScreenBo bo) {
        LambdaQueryWrapper<DsLargeScreen> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DsLargeScreen> buildQueryWrapper(DsLargeScreenBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DsLargeScreen> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLargeScreenName()), DsLargeScreen::getLargeScreenName, bo.getLargeScreenName());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), DsLargeScreen::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DsLargeScreen::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增大屏信息
     */
    @Override
    public Boolean insertByBo(DsLargeScreenBo bo) {
        DsLargeScreen add = BeanUtil.toBean(bo, DsLargeScreen.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLargeScreenId(add.getLargeScreenId());
        }
        return flag;
    }

    /**
     * 修改大屏信息
     */
    @Override
    public Boolean updateByBo(DsLargeScreenBo bo) {
        DsLargeScreen update = BeanUtil.toBean(bo, DsLargeScreen.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DsLargeScreen entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除大屏信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
